/******************************************************************************
* @file    i2c.c
* @author  
* @version 
* @date   
* @brief   This file contains all the functions for the I2C peripheral.
*******************************************************************************
* @attention
*
******************************************************************************/

/* Includes ------------------------------------------------------------------*/
#include "drivers.h"

/* Private typedef -----------------------------------------------------------*/

/* Private define ------------------------------------------------------------*/

/* Private macro -------------------------------------------------------------*/

/* Private variables ---------------------------------------------------------*/

/* Private function prototypes -----------------------------------------------*/

/* Private functions ---------------------------------------------------------*/

/* Public functions ----------------------------------------------------------*/


/******************************************************************************
* @function         : 
* @description     :   
* @Algorithm 	      : 
* @input              : 
* @output           : 
* @Vision        
*   	date   	:	
*   	author  	:
*   	detail  	:
******************************************************************************/ 
void InitDelay(void)
{
	/* enable dlTimer */
	//CLK_PeripheralClockConfig(CLK_PCKENR1_DLTIMER, ENABLE);

	/* DLTIMER Peripheral Configuration */ 
	//theDELAY_DeInit();

	/* dlTIMER_Prescaler = 0 */
	theDELAY->PSCR = 0;
	
	/* dlTimer enable */
	//dlTIMER_Cmd(ENABLE);
}

/******************************************************************************
* @function         : 
* @description     :   
* @Algorithm 	      : 
* @input              : 
* @output           : 
* @Vision        
*   	date   	:	
*   	author  	:
*   	detail  	:
******************************************************************************/ 
void _delay(u16 t)
{
	theDELAY->ARRH = (u8)(t >> 8);
	theDELAY->ARRL = (u8)(t);
	theDELAY->CR1 	|= 	0x01;
	while ( !(theDELAY->SR1 & theDELAY_SR1_UIF) );
	theDELAY->CR1	&= 	~0x01;
	theDELAY->SR1 	&= ~0x01;
}

 /******************************************************************************
 * @function         : 
 * @description     :   
 * @Algorithm          : 
 * @input              : 
 * @output           : 
 * @Vision        
 *       date    :   
 *       author      :
 *       detail      :
 ******************************************************************************/
void _delay_us(u16 us)
{
	us <<= 4;
	theDELAY->ARRH = (u8)(us >> 8);
	theDELAY->ARRL = (u8)(us);
	theDELAY->CR1 	|= 	0x01;
	while ( !(theDELAY->SR1 & theDELAY_SR1_UIF) );
	theDELAY->CR1	&= 	~0x01;
	theDELAY->SR1 	&= ~0x01;
}

/******************************************************************************
* @function         : 
* @description     :   
* @Algorithm 	      : 
* @input              : 
* @output           : 
* @Vision        
*   	date   	:	
*   	author  	:
*   	detail  	:
******************************************************************************/ 
void _delay_ms(u16 ms)
{

	theDELAY->ARRH = (u8)((u16)(MASTER_FREQUENCY * 1000) >> 8);
	theDELAY->ARRL = (u8)(MASTER_FREQUENCY * 1000);
	theDELAY->CR1 	|= 	0x01;
	while ( ms-- )
	{
		while ( !(theDELAY->SR1 & theDELAY_SR1_UIF) );
		theDELAY->SR1 &= ~theDELAY_SR1_UIF;
	}
	theDELAY->CR1	&= 	~0x01;
}
